package com.chenyue.cm.payment.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.chenyue.cm.base.aop.Log;
import com.chenyue.cm.base.bean.PageVo;
import com.chenyue.cm.base.bean.Pageable;
import com.chenyue.cm.bean.Success;
import com.chenyue.cm.common.utils.RequestUtil;
import com.chenyue.cm.order.domain.JdProduct;
import com.chenyue.cm.order.domain.JdProductExample;
import com.chenyue.cm.payment.service.JdProductService;
import com.chenyue.cm.user.domain.JdThirdInformation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Calendar;
import java.util.List;

/**
 * @author libingxing
 * @date 2021-07-07
 */
@RestController
@RequestMapping("/jdProduct")
public class JdProductController {

    private final JdProductService jdProductService;

    public JdProductController(JdProductService jdProductService) {
        this.jdProductService = jdProductService;
    }

    @RequestMapping("/getList")
    public Success getList(HttpServletRequest request) {
        Pageable pageInfo = RequestUtil.getPageable(request);
		PageVo<JdProduct>  result  = jdProductService.getPageBean(pageInfo, JdProduct.class);
		return Success.success(result);
    }

    /**
     * @param vo 插入实体
     *
     * @return Success
     */
    @Log
    @RequestMapping("/save")
    public Success save(JdProduct vo) {
        //如果是月卡充值，ios和安卓只能添加一个
        if(vo != null){
            if(vo.getType() == 2){
                LambdaQueryWrapper<JdProduct> wrapper = new LambdaQueryWrapper<>();
                wrapper.eq(JdProduct::getType,vo.getType());
                wrapper.eq(JdProduct::getDeviceModel,vo.getDeviceModel());
                List<JdProduct> jdProductsList = jdProductService.list(wrapper);
                if(jdProductsList != null && jdProductsList.size()>0){
                    return Success.error((vo.getDeviceModel() == 1 ? "安卓" : "ios" )+"月卡充值类型已存在,不可重复添加");
                }
            }
        }
        vo.setCt(Calendar.getInstance().getTime());
        boolean success = jdProductService.save(vo);
        return success ? Success.success() : Success.error();
    }

    /**
    * @param vo 修改实体
    * @param request id
    *
    * @return Success
    */
   @Log
   @RequestMapping("/edit")
   public Success edit(JdProduct vo, HttpServletRequest request) {
       String id = request.getParameter("id");
        if (StringUtils.isNotBlank(id)) {
            JdProduct jdProduct = jdProductService.getBaseMapper().selectById(id);
            if(vo.getType() == 2){
                if(jdProduct.getDeviceModel() != vo.getDeviceModel()){
                    //校验是否存在
                    LambdaQueryWrapper<JdProduct> wrapper = new LambdaQueryWrapper<>();
                    wrapper.eq(JdProduct::getType,vo.getType());
                    wrapper.eq(JdProduct::getDeviceModel,vo.getDeviceModel());
                    List<JdProduct> jdProductsList = jdProductService.list(wrapper);
                    if(jdProductsList != null && jdProductsList.size()>0){
                        return Success.error((vo.getDeviceModel() == 1 ? "安卓" : "ios" )+"月卡充值类型已存在,不可重复添加");
                    }
                }
            }
            int success = jdProductService.getBaseMapper().updateById(vo);
            return success > 0 ? Success.success() : Success.error();
        } else {
          return Success.error("id不能为空");
        }
    }

    /**
     *
     * @param id 主键
     *
     * @return Success
     */
    @Log
    @RequestMapping("/delete/{id}")
    public Success deleteByPrimaryKey(@PathVariable("id") Integer id) {
        JdProductExample aExample = new JdProductExample();
        aExample.createCriteria().andIdEqualTo(id);
        int success = jdProductService.deleteByExample(aExample);
        return success > 0 ? Success.success() : Success.error();
    }

 	/**
	 * 【API】----  删除多个数据
     *
	 * @return Success
	 */
    @Log
    @RequestMapping("/delete")
	public Success delete(String ids) {
        String[] idStrings = ids.split(",");
        for (String idString : idStrings) {
            int success = jdProductService.deleteByPrimaryKey(idString);
            if (success == 0) {
                return Success.error();
            }
        }
        return Success.success();
	}

}
